package com.solution.admin.order.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.solution.admin.order.entity.OrderDeliveredBillEntity;
import com.solution.admin.order.entity.OrderDetailEntity;
import com.solution.admin.order.service.OrderDetailService;
import com.solution.admin.product.entity.ProductSpecEntity;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 订单商品详情web控制层
 * @author lilp
 * @date 2017-09-15 18:31:33
 */
@RequestMapping("/orderDetail")
@Controller
public class OrderDetailController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderDetailController.class);

	@Autowired
	private OrderDetailService orderDetailService;
	
	/**
	 * 跳转订单商品详情查询页面
	 * @param OrderDetail 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-15 18:31:33
	 */
	@RequestMapping("/toListOrderDetailPage")
	public String toListOrderDetailPage(OrderDetailEntity orderDetail, HttpServletRequest request) {
		return "order/orderDetail_list";
	}

	/**
	 * 分页查询订单商品详情列表
	 * @param orderDetail 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-15 18:31:33
	 */
	@RequestMapping("/listOrderDetailPage")
	@ResponseBody
	public JSONGrid listOrderDetailPage(OrderDetailEntity orderDetail, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = orderDetailService.listOrderDetailPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询订单商品详情失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 根据订单编号查询订单商品详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/listOrderDetailByOrderCode")
	@ResponseBody
	public JSONGrid listOrderDetailByOrderCode(HttpServletRequest request){
		// 订单编号 
		String orderCode = request.getParameter("orderCode");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		try {
			List<OrderDetailEntity> productList = orderDetailService.listOrderDetail(paramMap);
			if(productList != null && productList.size() > 0 ){
				for (OrderDetailEntity product : productList) {
					// 设置商品小计金额，如果有商品调价，否则按商品原价
					product.setProductItemMoney(product.getProductAdjustPrice().multiply(new BigDecimal(product.getBuyNum())));
				
					if(StringUtil.isNotEmpty(product.getProductSpec())){
						List<ProductSpecEntity> productSpecList = JSONArray.parseArray(product.getProductSpec(), ProductSpecEntity.class);;
						product.setProductSpecList(productSpecList);
					}
				}
			}
			return new JSONGrid(productList,productList.size());
		} catch (Exception e) {
			handleException("根据订单编号查询订单商品详情", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 查询订单已发货的商品
	 * @param request
	 * @return
	 */
	@RequestMapping("/listOrderDetailNotDelivered")
	@ResponseBody
	public JSONGrid listOrderDetailNotDelivered(HttpServletRequest request){
		// 订单编号 
		String orderCode = request.getParameter("orderCode");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		
		List<OrderDetailEntity>  deliveredList = new ArrayList<OrderDetailEntity>();
		try {
			List<OrderDetailEntity> productList = orderDetailService.listOrderDetail(paramMap);
			if(productList != null && productList.size() > 0 ){
				for (OrderDetailEntity product : productList) {
					if(product.getSendNum() < product.getBuyNum()){
						deliveredList.add(product);
					}
					if(StringUtil.isNotEmpty(product.getProductSpec())){
						List<ProductSpecEntity> productSpecList = JSONArray.parseArray(product.getProductSpec(), ProductSpecEntity.class);;
						product.setProductSpecList(productSpecList);
					}
				}
			}
			return new JSONGrid(deliveredList,deliveredList.size());
		} catch (Exception e) {
			handleException("查询订单已发货的商品", logger, e);
		}
		return new JSONGrid();
	}
	
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-15 18:31:33
	 */
	@RequestMapping("/toAddOrderDetail")
	public String toAddOrderDetail(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderDetail_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-15 18:31:33
	 */
	@RequestMapping("/toUpdateOrderDetail")
	public String toUpdateOrderDetail(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderDetailEntity orderDetail = orderDetailService.getOrderDetailById(id);
			request.setAttribute("orderDetail", orderDetail);
		} catch (Exception e) {
			handleException("跳转编辑订单商品详情页面错误", logger, e);
		}
		return "order/orderDetail_form";
	}


	/**
	 * 新增或保存订单商品详情
	 * @param request 请求对象
	 * @param orderDetailEntity 订单商品详情实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-15 18:31:33
	 */
	@RequestMapping("/saveOrUpdateOrderDetail")
	@ResponseBody
	public Map<String, Object> saveOrderDetail(HttpServletRequest request, OrderDetailEntity orderDetailEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isEmpty(id)){
				result = orderDetailService.updateOrderDetail(orderDetailEntity);
			}else{
				result = (int)orderDetailService.saveOrderDetail(orderDetailEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加订单商品详情失败", logger, e);
		}
		return resultMap;
	}
	
}
